package com.huaxin.hxcloud.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.huaxin.common.BaseActivity;
import com.huaxin.common.Constants;
import com.huaxin.common.Entity.Info;
import com.huaxin.common.Entity.InfoOrTypeListDTO;
import com.huaxin.common.view.pulltorefresh.PullToRefreshBase;
import com.huaxin.common.view.pulltorefresh.extras.PullToRefreshWebView2;
import com.huaxin.hxcloud.R;

import butterknife.BindView;

/**
 * Created by Administrator on 2016/10/26.
 */

public class CommonWebActivity extends BaseActivity implements PullToRefreshBase
        .OnRefreshListener<WebView> {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.pull_view)
    PullToRefreshWebView2 pullView;
    private String mUrl;
    private String mTitle;
    private WebView webView;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_info;
    }

    @Override
    public void initView() {
        super.initView();
        setSupportActionBar(toolbar);
        toolbar.setTitle(mTitle);
        webView = pullView.getRefreshableView();
        pullView.setOnRefreshListener(this);

        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setUseWideViewPort(true);//关键点
        webSettings.setDisplayZoomControls(false);
        webSettings.setJavaScriptEnabled(true); // 设置支持javascript脚本
        webSettings.setAllowFileAccess(true); // 允许访问文件
        webSettings.setBuiltInZoomControls(true); // 设置显示缩放按钮
        webSettings.setSupportZoom(true); // 支持缩放
        webSettings.setLoadWithOverviewMode(true);
//

        /**
         * 用WebView显示图片，可使用这个参数 设置网页布局类型： 1、LayoutAlgorithm.NARROW_COLUMNS ：
         * 适应内容大小 2、LayoutAlgorithm.SINGLE_COLUMN:适应屏幕，内容将自动缩放
         */
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webView.setWebChromeClient(new WebChromeClient());
        webView.setWebViewClient(new MyWebViewClient());
        webView.loadUrl(mUrl);
//        webView.loadUrl("file:///android_asset/e.html");

        mUrl = getIntent().getExtras().getString(Constants.URL_KEY);
        mTitle = getIntent().getExtras().getString(Constants.TITLE_KEY);
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
        mUrl = extras.getString(Constants.URL_KEY);
        mTitle = extras.getString(Constants.TITLE_KEY);
    }

    public static void start(Context context, InfoOrTypeListDTO.InfosBean info) {
        Intent intent = new Intent(context, CommonWebActivity.class);
        intent.putExtra(Constants.URL_KEY, info.getUrl());
        intent.putExtra(Constants.TITLE_KEY, info.getTitle());
        context.startActivity(intent);
    }


    private static class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
//            view.loadUrl(url);
//            return true;
            return false;
        }

    }

    @Override
    public void onRefresh(final PullToRefreshBase<WebView> refreshView) {
        // This is very contrived example, we just wait 2 seconds, then call
        // onRefreshComplete()
        refreshView.postDelayed(new Runnable() {
            @Override
            public void run() {
                refreshView.onRefreshComplete();
            }
        }, 2 * 1000);
    }

    @Override
    public void onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack(); // goBack()表示返回WebView的上一页面
        } else {
            super.onBackPressed();
        }

    }
}
